//////////////////////////////////////////////
//                                          //
//          Alexander K. Haley's            //
//            Space Warz Game               //
//    (For Prototype Hardware, w/o CPU)     //
//                                          //
//                Build #2                  //
//           Last edit 5/2/2020             //
//        Originally made 5/2/2020          //
//                                          //
//        (c) Alexander K. Haley            //
//                  of                      //
//            Kincaid Arcade                //
//                                          //
//////////////////////////////////////////////

//One thing I would certainly change if I did this again would be to rearrange pins to facilitate port manipulation.
//Also use a DUE.
//Seriously. An Arduino Mega has a hard time keeping up. (A measly 39 fps!)

//Added the two ships!
//They turn, thrust, hyperspace, and wrap around the screen!

//address pins: a0-a10 = pin 22-32
//data pins: q0-q11 = pin 38-39
//misc pins:
//  processorready = pin 32
//  processoroverride = pin 7
//  writeenable = pin 34
//  outputenable = pin 35
//  chipselect = pin 36
//  framecomplete = pin 37  INPUT
//LED debugging pins:
//  ledone = pin 12
//  ledtwo = pin 11
//  ledthree = pin 10
//  errorled = pin 9

byte azero  = 22;
byte aone   = 23;
byte atwo   = 24;
byte athree = 25;
byte afour  = 26;
byte afive  = 27;
byte asix   = 28;
byte aseven = 29;
byte aeight = 30;
byte anine  = 31;
byte aten   = 32;

//data pins
byte qzero     = 38;
byte qone      = 39;
byte qtwo      = 40;
byte qthree    = 41;
byte qfour     = 42;
byte qfive     = 43;
byte qsix      = 44;
byte qseven    = 45;
byte qeight    = 46;
byte qnine     = 47;
byte qten      = 48;
byte qeleven   = 49;
//byte qtwelve   = 50; Technically, they aren't ever used...
//byte qthirteen = 51;
//byte qfourteen = 52;
//byte qfifteen  = 53;

//switch input pins
byte ptwofire   = A0; 
byte ptwothrust = A1;
byte ptwohyperspace = A2;
byte ptworight  = A3;
byte ptwoleft   = A4;

byte startbutton = A8;

byte ponefire   = A9;
byte ponethrust = A10;
byte ponehyperspace = A11; 
byte poneright  = A12;
byte poneleft   = A13;


//misc. pins
byte processorready = 33;

byte writeenable    = 34;
byte outputenable   = 35;
byte chipselect     = 36;
byte framecomplete  = 37;

byte processoroverride = 7;

byte ledone = 12;
byte ledtwo = 11;
byte ledthree = 10;
byte errorled = 9;

//Frame Buffer
int xcoords[600];
int ycoords[600];
int zvalues[600];


//variables
int currentaddress  = 1;
int internaladdress = 0;
int mathaddress     = 0;

int transferaddress = 0;

int currentdata = 0;
int mathdata    = 0;

byte bank = 0;
byte portdata = 0;

byte frame = 0; //used for indicating whether or not a processoroverride is necessary.

byte lastswitchstates[12];
byte currentswitchstates[12];
byte hitswitches[12];

//timing
unsigned long lastmillis = 0;
unsigned long currentmillis = 0;

//positions & other variables
//p1 ship
int alienxpos = 0;
int alienypos = 0;

float alienrotation = 0.0; //radians
int   aliendx = 0;
int   aliendy = 0;

float alienvelocity = 0.0;

int randomalienx = 0;
int randomalieny = 0;

//p2 ship
int rocketxpos = 256;
int rocketypos = 512;

float rocketrotation = 0.0; //Also in radians
int   rocketdx = 0;
int   rocketdy = 0;

float rocketvelocity = 0.0;

int randomrocketx = 0;
int randomrockety = 0;

/////////////////////////////////////////////////
//                                             //
//                                             //
//      Game Object Coordinates & Buffers      //
//                                             //
//                                             //
/////////////////////////////////////////////////

//star
int starx[9] = {0,  64, 56, 8,  32, 32, 8, 56};
int stary[9] = {32, 32, 8,  56, 64, 0,  8, 56};
int starz[9] = {0,  15, 0,  15, 0,  15, 0, 15};

int starxbuffer[9];
int starybuffer[9];
int starzbuffer[9];

//border
int borderx[6] = {0,   0,   1023, 1023,  0};
int bordery[6] = {128, 896, 896,  128,   128};
int borderz[6] = {0,   15,  15,   15,    15};

//p1 alien ship
float alienangle[19]    = {4.248, 4.390, 4.712, 1.951, 1.670, 2.035, 3.142, 4.712, 0.000, 1.107, 1.471, 1.190, 0.000, 5.034, 5.177, 4.248, 4.468, 4.712, 4.957}; //In radians :/
float aliendistance[19] = {26.8,  18.9,  18.0,  32.3,  30.1,  6.7,   6.0,   6.0,   6.0,   6.7,   30.1,  32.3,  18.0,  19.0,  26.8,  26.8,  24.7,  42.0,  24.7}; 
int   alienz[19]        = {0,     15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    0,     15,    15}; //the last two z values correspond to the thrust spray

float newalienangle[19] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

int alienxbuffer[19];
int alienybuffer[19];
int alienzbuffer[19];

//p2 rocket ship
float rocketangle[19]    = {4.391, 4.332, 3.927, 3.606, 2.677, 1.571, 0.464, 5.819, 5.498, 5.093, 5.034, 4.391, 4.712, 5.034, 0.000, 3.142, 2.035, 1.107, 0.000}; //Also in radians :/
float rocketdistance[19] = {19.0,  32.3,  25.5,  13.4,  13.4,  24.0,  13.4,  13.4,  25.5,  32.3,  19.0,  19.0,  42.0,  19.0,  3.0,   3.0,   6.7,   6.7,   3.0}; 
int   rocketz[19]        = {0,     15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    15,    0,     15,    15,    15,    15}; //0-indexed 12 & 13 correspond to thrust spray

float newrocketangle[19] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

int rocketxbuffer[19];
int rocketybuffer[19];
int rocketzbuffer[19];


void setup() {
//Serial.begin(9600); //only needed for testing stuff

//The ports on the Mega are weird...
DDRA = DDRA | B11111111;
DDRB = DDRB | B01110000;
DDRC = DDRC | B11111110;
DDRD = DDRD | B10000000;
DDRG = DDRG | B00000111;
DDRH = DDRH | B01010000;
DDRL = DDRL | B11111111;

pinMode(ponefire,INPUT);
pinMode(ponethrust,INPUT);
pinMode(ponehyperspace,INPUT);
pinMode(poneright,INPUT);
pinMode(poneleft,INPUT);

pinMode(startbutton,INPUT);

pinMode(ptwofire,INPUT);
pinMode(ptwothrust,INPUT);
pinMode(ptwohyperspace,INPUT);
pinMode(ptworight,INPUT);
pinMode(ptwoleft,INPUT);

digitalWrite(chipselect,LOW);
digitalWrite(outputenable,HIGH); //Make sure we're in high impedance mode.
digitalWrite(writeenable,HIGH);

randomSeed(analogRead(15));
}

void loop() {
lastmillis = millis(); //Remember when the frame was begun, so we can know if it took too long. If too short, the program will stall until the frame has taken 16ms. (Throttling fps to ~60 max.) 

readswitches();
//apply game logic
dogamelogic();
//manipulate game objects xy -- basically all the transformations to make to the objects before drawing to the screen.
manipulateobjectxy();

//optimize drawing order based on screen "zone" and write to buffer.
//optimizedraworder
writetomasterbuffer();

writeobjectstoram();
//Check time to complete to millis watchdog. - Light LED if too slow.
vgenchecker();
}

////////////////////////////////////////////////
//                                            //
//                                            //
//       This section deals with all          //
//    of the x & y object transformations     //
//                                            //
//                                            //
////////////////////////////////////////////////

void manipulateobjectxy()
{
  manipulatestarxy();
  manipulaterocketshipxy();
  manipulatealienxy();
}

void manipulatestarxy()
{
  //width of star 64 by 64  
  //offsets star to the center of the screen.
  for(int i=0; i<9; i++)
    {
      starxbuffer[i] = starx[i] + 480;
      starybuffer[i] = stary[i] + 480;
      starzbuffer[i] = starz[i];  
    }
}

void manipulaterocketshipxy()
{
  for(int i=0; i<19; i++) //Change all thetas to match rotation
    {
      newrocketangle[i] = rocketangle[i]-1.571+rocketrotation; //the -1.571 is to compensate for the ship images being pointed at 90 degrees
    }

  for(int i=0; i<19; i++)
    {
      rocketxbuffer[i] = rocketdistance[i]*cos(newrocketangle[i]); 
      rocketybuffer[i] = rocketdistance[i]*sin(newrocketangle[i]); 
      rocketzbuffer[i] = rocketz[i]; 
    }

  for(int i=0; i<19; i++) //Shift the position of everything
    {
      rocketxbuffer[i] += rocketxpos;
      rocketybuffer[i] += rocketypos;
    }

   //add the stuff for turning on and off the flame
}

void manipulatealienxy()
{
  for(int i=0; i<19; i++) //Change all thetas to match rotation
    {
      newalienangle[i] = alienangle[i]-1.571+alienrotation; //the -1.571 is to compensate for the ship images being pointed at 90 degrees
    }

  for(int i=0; i<19; i++)
    {
      alienxbuffer[i] = aliendistance[i]*cos(newalienangle[i]); 
      alienybuffer[i] = aliendistance[i]*sin(newalienangle[i]); 
      alienzbuffer[i] = alienz[i]; 
    }

  for(int i=0; i<19; i++) //Shift the position of everything
    {
      alienxbuffer[i] += alienxpos;
      alienybuffer[i] += alienypos;
    }

   //add the stuff for turning on and off the flame
}

////////////////////////////////////////////////
//                                            //
//                                            //
//       This section deals with the          //
//                game logic                  //
//                                            //
//                                            //
////////////////////////////////////////////////

void dogamelogic()
{
  //p2 rocket pos & rot stuff
  if(currentswitchstates[1] == 1) //P2 thrust  
    {
      rocketvelocity += 4.0; //gonna have to figure out better thrust stuff  
    }
  else if(currentswitchstates[1] == 0 && rocketvelocity > 0.0)
    {
      rocketvelocity -= 1.0;  
    }
 
  if(rocketvelocity > 4.0) //don't let it move more than 240 pixels per second (1/4) screen
    {
      rocketvelocity = 4.0;  
    }

  if(currentswitchstates[3] == 1) //P2 Right
    {
      rocketrotation -= 0.07; //I hate radians
    }
  if(currentswitchstates[4] == 1) //P2 Left
    {
      rocketrotation += 0.07; //Normal people would say "4 degrees"
    }

  //make sure the rocket is always at a known angle.
  if(rocketrotation > 6.283)
    {
      rocketrotation = 0;
    }
  if(rocketrotation < 0)
    {
      rocketrotation = 6.283;  
    }

  //calculate directional movement based on angle and velocity;
  rocketdx = rocketvelocity*cos(rocketrotation);
  rocketdy = rocketvelocity*sin(rocketrotation);

  //move the ship
  rocketxpos += rocketdx;
  rocketypos += rocketdy;

  //make sure it's center isn't too close to the edge of the screen. If it is, have it wrap around.
  if(rocketxpos < 50)
    {
      rocketxpos = 973;  
    }
  if(rocketxpos > 973)
    {
      rocketxpos = 50;  
    }
  if(rocketypos < 178)
    {
      rocketypos = 846;  
    }
  if(rocketypos > 846)
    {
      rocketypos = 178;
    }

  //Hyperspace!
  if(hitswitches[2] == 1) //P2 Hyperspace
    {
       randomrocketx = random(50, 974);
       randomrockety = random(178, 846);

       rocketxpos = randomrocketx;
       rocketypos = randomrockety;
    }
  
  
  //p1 alien pos & rot stuff
  if(currentswitchstates[7] == 1) //P1 thrust  
    {
      alienvelocity += 4.0; //gonna have to figure out better thrust stuff  
    }
  else if(currentswitchstates[7] == 0 && alienvelocity > 0.0)
    {
      alienvelocity -= 1.0;  
    }
 
  if(alienvelocity > 4.0) //don't let it move more than 240 pixels per second (1/4) screen
    {
      alienvelocity = 4.0;  
    }

  if(currentswitchstates[9] == 1) //P2 Right
    {
      alienrotation -= 0.07; //I hate radians
    }
  if(currentswitchstates[10] == 1) //P2 Left
    {
      alienrotation += 0.07; //Normal people would say "4 degrees"
    }

  //make sure the rocket is always at a known angle.
  if(alienrotation > 6.283)
    {
      alienrotation = 0;
    }
  if(alienrotation < 0)
    {
      alienrotation = 6.283;  
    }

  //calculate directional movement based on angle and velocity;
  aliendx = alienvelocity*cos(alienrotation);
  aliendy = alienvelocity*sin(alienrotation);

  //move the ship
  alienxpos += aliendx;
  alienypos += aliendy;

  //make sure it's center isn't too close to the edge of the screen. If it is, have it wrap around.
  if(alienxpos < 50)
    {
      alienxpos = 973;  
    }
  if(alienxpos > 973)
    {
      alienxpos = 50;  
    }
  if(alienypos < 178)
    {
      alienypos = 846;  
    }
  if(alienypos > 846)
    {
      alienypos = 178;
    }

  //Hyperspace!
  if(hitswitches[8] == 1) //P1 Hyperspace
    {
       randomalienx = random(50, 974);
       randomalieny = random(178, 846);

       alienxpos = randomalienx;
       alienypos = randomalieny;
    }

  
}

////////////////////////////////////////////////
//                                            //
//          This section deals with           //
//   moving the buffered coordinates to the   //
//      master buffer, so it can then be      //
//          written to the RAM chips.         //
//                                            //
////////////////////////////////////////////////
void writetomasterbuffer()
{
   transferaddress = 0;
   
   //load the border around the screen
   //not necessary for now.
//   for(int i=0; i<6; i++)
//    {
//      xcoords[transferaddress] = borderx[i];
//      ycoords[transferaddress] = bordery[i];
//      zvalues[transferaddress] = borderz[i];
//      transferaddress++;  
//    }

   //load the star
   for(int i=0; i<9; i++)
    {
      xcoords[transferaddress] = starxbuffer[i];
      ycoords[transferaddress] = starybuffer[i];
      zvalues[transferaddress] = starzbuffer[i];
      transferaddress++;  
    }

   //load the p2 rocket ship
   for(int i=0; i<19; i++)
    {
      xcoords[transferaddress] = rocketxbuffer[i];  
      ycoords[transferaddress] = rocketybuffer[i]; 
      zvalues[transferaddress] = rocketzbuffer[i]; 
      transferaddress++;
    }

  //load the p1 alien vessel
   for(int i=0; i<19; i++)
    {
      xcoords[transferaddress] = alienxbuffer[i];  
      ycoords[transferaddress] = alienybuffer[i]; 
      zvalues[transferaddress] = alienzbuffer[i]; 
      transferaddress++;
    }

   //then, at the end, have a coorinate with a Z value of 128 to signal the end of the frame.
    xcoords[transferaddress] = xcoords[transferaddress-1];
    ycoords[transferaddress] = ycoords[transferaddress-1];
    zvalues[transferaddress] = 128;
}

////////////////////////////////////////////////
//                                            //
//                                            //
//       Miscellanneous control stuff         //
//                                            //
//                                            //
////////////////////////////////////////////////

void vgenchecker()
{
  if(frame == 0)
    {
      initiateprocessoroverride();  
      frame = 1;
      return;
    }
  while(digitalRead(framecomplete) == LOW)
    {
      delayMicroseconds(1);  
      
      //save this for implementing the watchdog.
      /*currentmillis = millis();
      if(currentmillis > lastmillis + 16)
        {
            
        }*/ 
    }
  if(digitalRead(framecomplete) == HIGH)
    {
      currentmillis = millis();
      //The throttle is disabled since the frame doesn't take close to the 16ms, which causes the sample&hold issues.
      /*while(currentmillis < lastmillis + 16) //this should throttle it down to ~60 fps tops. 
        {
          delayMicroseconds(1);
          currentmillis = millis(); //otherwise it gets stuck in this loop.
        }*/
   
      //digitalWrite(processorready,HIGH);
      PORTC = PORTC | B00010000;
      //digitalWrite(processorready,LOW);
      PORTC = PORTC & B11101111;
    }  
}

void readswitches()
{
  currentswitchstates[0] = digitalRead(ptwofire);
  currentswitchstates[1] = digitalRead(ptwothrust);
  currentswitchstates[2] = digitalRead(ptwohyperspace);
  currentswitchstates[3] = digitalRead(ptworight);
  currentswitchstates[4] = digitalRead(ptwoleft);

  currentswitchstates[5] = digitalRead(startbutton);
  
  currentswitchstates[6] = digitalRead(ponefire);
  currentswitchstates[7] = digitalRead(ponethrust);
  currentswitchstates[8] = digitalRead(ponehyperspace);
  currentswitchstates[9] = digitalRead(poneright);
  currentswitchstates[10] = digitalRead(poneleft);

  for(byte x=0; x<11; x++)
    {
      if(currentswitchstates[x] == 1 && lastswitchstates[x] == 0)
        {
          hitswitches[x] = 1;
        }
      else
        {
          hitswitches[x] = 0;  
        }
    }

  for(byte x=0; x<11; x++)
    {
      lastswitchstates[x] = currentswitchstates[x];  
    }
/*
  if(hitswitches[0] == 1) //P2 Fire button
    {
      Serial.println("P2 fired");  
    }
  if(hitswitches[1] == 1) //P2 Thrust
    {
      Serial.println("P2 applied thrust");  
    }
  if(hitswitches[2] == 1) //P2 Hyperspace
    {
      Serial.println("P2 entered hyperspace");  
    }
  if(hitswitches[3] == 1) //P2 Right
    {
      Serial.println("P2 turned right");  
    }
  if(hitswitches[4] == 1) //P2 Left
    {
      Serial.println("P2 turned left");  
    }
  if(hitswitches[5] == 1) //Start
    {
      Serial.println("The start button was pressed.");  
    }
  if(hitswitches[6] == 1) //P1 Fire
    {
      Serial.println("P1 Fired");  
    }
  if(hitswitches[7] == 1) //P1 Thrust
    {
      Serial.println("P1 applied thrust");  
    }
  if(hitswitches[8] == 1) //P1 Hyperspace
    {
      Serial.println("P1 hyperspaced");  
    }
  if(hitswitches[9] == 1) //P1 Right
    {
      Serial.println("P1 turned right");  
    }
  if(hitswitches[10] == 1) //P1 Left
    {
      Serial.println("P1 turned left");  
    }
*/
}

///////////////////////////////////////////
//                                       //
//       This section deals with         //
//         handling all the RAM          //
//          writing and stuff            //
//                                       //
///////////////////////////////////////////

void writeobjectstoram()
{
  while(zvalues[internaladdress] != 128)
    {
      computeaddress();
      computedata();
      writedata();
      currentaddress++;
    }
  currentaddress = 1;
  internaladdress = 0;
  bank = 0;
}

void computeaddress()
{
  //split address into zeros and ones
  mathaddress = currentaddress;
  
  if(mathaddress >= 1024)
    {
      //digitalWrite(aten,HIGH); 
      PORTC = PORTC | B00100000;
      mathaddress -= 1024; 
    }
  else
    {
      //digitalWrite(aten,LOW);
      PORTC = PORTC & B11011111;
    }
    
  if(mathaddress >= 512)
    {
      //digitalWrite(anine,HIGH); 
      PORTC = PORTC | B01000000; 
      mathaddress -= 512; 
    }
  else
    {
      //digitalWrite(anine,LOW);
      PORTC = PORTC & B10111111;
    }
    
  if(mathaddress >= 256)
    {
      //digitalWrite(aeight,HIGH);  
      PORTC = PORTC | B10000000;
      mathaddress -= 256; 
    }
  else
    {
      //digitalWrite(aeight,LOW);
      PORTC = PORTC & B01111111; 
    }

  portdata = mathaddress;
  PORTA = portdata;
}

void computedata()
{
  if(bank == 3)
    {
      bank = 0;  
      internaladdress ++;
    }
  
  if(bank == 0) //x bank
    {
      currentdata = xcoords[internaladdress];
    }
  else if(bank == 1) //y bank
    {
      currentdata = ycoords[internaladdress];
    }
  else if(bank == 2) //zbank
    {
      currentdata = zvalues[internaladdress];
    }

  mathdata = currentdata;

  if(mathdata >= 2048)
    {
      //digitalWrite(qeleven,HIGH); 
      PORTL = PORTL | B00000001;
      mathdata -= 2048; 
    }
  else
    {
      //digitalWrite(qeleven,LOW);
      PORTL = PORTL & B11111110;
    }

  if(mathdata >= 1024)
    {
      //digitalWrite(qten,HIGH); 
      PORTL = PORTL | B00000010;
      mathdata -= 1024; 
    }
  else
    {
      //digitalWrite(qten,LOW);
      PORTL = PORTL & B11111101;
    }
    
  if(mathdata >= 512)
    {
      //digitalWrite(qnine,HIGH);
      PORTL = PORTL | B00000100;  
      mathdata -= 512; 
    }
  else
    {
      //digitalWrite(qnine,LOW);
      PORTL = PORTL & B11111011;
    }
    
  if(mathdata >= 256)
    {
      //digitalWrite(qeight,HIGH);  
      PORTL = PORTL | B00001000;
      mathdata -= 256; 
    }
  else
    {
      //digitalWrite(qeight,LOW);
      PORTL = PORTL & B11110111;
    }
    
  if(mathdata >= 128)
    {
      //digitalWrite(qseven,HIGH);  
      PORTL = PORTL | B00010000;
      mathdata -= 128; 
    }
  else
    {
      //digitalWrite(qseven,LOW);
      PORTL = PORTL & B11101111;
    }
    
  if(mathdata >= 64)
    {
      //digitalWrite(qsix,HIGH);  
      PORTL = PORTL | B00100000;
      mathdata -= 64; 
    }
  else
    {
      //digitalWrite(qsix,LOW);
      PORTL = PORTL & B11011111;
    }
    
  if(mathdata >= 32)
    {
      //digitalWrite(qfive,HIGH); 
      PORTL = PORTL | B01000000; 
      mathdata -= 32; 
    }
  else
    {
      //digitalWrite(qfive,LOW);
      PORTL = PORTL & B10111111;
    }
    
  if(mathdata >= 16)
    {
      //digitalWrite(qfour,HIGH);  
      PORTL = PORTL | B10000000;
      mathdata -= 16; 
    }
  else
    {
      //digitalWrite(qfour,LOW);
      PORTL = PORTL & B01111111;
    }
    
  if(mathdata >= 8)
    {
      //digitalWrite(qthree,HIGH);  
      PORTG = PORTG | B00000001;
      mathdata -= 8; 
    }
  else
    {
      //digitalWrite(qthree,LOW);
      PORTG = PORTG & B11111110;
    }
    
  if(mathdata >= 4)
    {
      //digitalWrite(qtwo,HIGH); 
      PORTG = PORTG | B00000010; 
      mathdata -= 4; 
    }
  else
    {
      //digitalWrite(qtwo,LOW);
      PORTG = PORTG & B11111101;
    }
    
  if(mathdata >= 2)
    {
      //digitalWrite(qone,HIGH);  
      PORTG = PORTG | B00000100;
      mathdata -= 2; 
    }
  else
    {
      //digitalWrite(qone,LOW);
      PORTG = PORTG & B11111011;
    }
    
  if(mathdata >= 1)
    {
      //digitalWrite(qzero,HIGH);  
      PORTD = PORTD | B10000000;
      mathdata -= 1; 
    }
  else
    {
      //digitalWrite(qzero,LOW);
      PORTD = PORTD & B01111111;
    }

  if(mathdata != 0)
    {
      //digitalWrite(errorled,HIGH);  
    }
}

void writedata()
{
  //digitalWrite(writeenable,LOW);
  //digitalWrite(writeenable,HIGH);
  PORTC = PORTC & B11110111; //why does this work? Well, each port manipulation takes 4.9 us, which is 4900 nanoseconds.
  PORTC = PORTC | B00001000; //That may seem like a long time, but if you consider that each digitalWrite takes up to 2 milliseconds...
  bank ++;
}

void initiateprocessoroverride()
{
  //digitalWrite(processoroverride, HIGH);
  PORTH = PORTH | B00010000;
  //digitalWrite(processorready, HIGH);
  PORTC = PORTC | B00010000;
  //digitalWrite(processoroverride, LOW);
  PORTH = PORTH & B11101111;
  //digitalWrite(processorready, LOW);  
  PORTC = PORTC & B11101111;
}
